package com.suixun.queue;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.lib.httpsqs4j.HttpsqsClient;
import com.suixun.lib.httpsqs4j.HttpsqsException;

/*
 * 监听 找手表 录音命令 listenin find audio
 */
public class CMDDeal{
    @Resource
    private HttpsqsClient syssqs;
    private static ConcurrentLinkedQueue<Map<String, String>> queue =
            new ConcurrentLinkedQueue<Map<String, String>>();

    public static void send(String name, Integer peopleid, Integer objectid, String telephone,
            String mtdid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        map.put("telephone", telephone);
        map.put("mtdid", mtdid);
        queue.offer(map);
    }

    public static void sendcancel(String name, Integer peopleid, Integer objectid,
            String telephone, String mtdid, String babyname) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        map.put("telephone", telephone);
        map.put("mtdid", mtdid);
        map.put("babyname", babyname);
        queue.offer(map);
    }

    public static void send(String name, Integer peopleid, Integer objectid, String telephone,
            String familystatuses, String mtdid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        map.put("telephone", telephone);
        map.put("familystatuses", familystatuses);
        map.put("mtdid", mtdid);
        queue.offer(map);
    }

    public static void sendshutdown(String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "remoteshutdown");
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        map.put("mdtid", mdtid);
        queue.offer(map);
    }

    public static void sendreportmode(Integer mode, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "reportmode");
        map.put("mdtid", mdtid);
        map.put("mode", mode.toString());
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendfirewall(Boolean firewall, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "firewall");
        map.put("status", firewall ? "on" : "off");
        map.put("mdtid", mdtid);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendpowerbtn(Boolean powerbtn, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "powerbtn");
        map.put("status", powerbtn ? "on" : "off");
        map.put("mdtid", mdtid);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendbluetooth(Boolean bluetooth, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "bluetooth");
        map.put("status", bluetooth ? "on" : "off");
        map.put("mdtid", mdtid);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendclasssilent(String json, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "classsilent");
        map.put("data", json);
        map.put("mdtid", mdtid);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendalarmclock(String json, String mdtid,Integer peopleid, Integer objectid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "alarmclock");
        map.put("data", json);
        map.put("mdtid", mdtid);
        map.put("peopleid", peopleid.toString());
        map.put("objectid", objectid.toString());
        queue.offer(map);
    }

    public static void sendrenewal(String mdtid, String endtime, String telephone,BigDecimal fee,Date datetime) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "renewal");
        map.put("endtime", endtime);
        map.put("mdtid", mdtid);
        map.put("telephone", telephone);
        map.put("fee", fee.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        map.put("datetime", sdf.format(datetime));
        queue.offer(map);
    }
    public static void sendpurchased(String telephone,
                                     Date datetime,Integer num1,Integer num2,BigDecimal price1,BigDecimal price2){
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "purchase");
        map.put("telephone", telephone);
        map.put("num1", num1.toString());
        map.put("num2", num2.toString());
        map.put("price1", price1.toString());
        map.put("price2", price2.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        map.put("datetime", sdf.format(datetime));
        queue.offer(map);
    }
    
    public static void sendnotice(String mdtid, String notice, String teachername,
            String studentname) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "notice");
        map.put("mdtid", mdtid);
        map.put("notice", notice);
        map.put("teachername", teachername);
        map.put("studentname", studentname);
        queue.offer(map);
    }

    public static void sendcarefor(String mdtid, Integer type, String teachername,
            String studentname) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "carefor");
        map.put("mdtid", mdtid);
        map.put("type", type.toString());
        map.put("teachername", teachername);
        map.put("studentname", studentname);
        queue.offer(map);
    }

    public static void sendcarefor(String mdtid, Integer type, String teachername,
            String studentname, String teachertel) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "carefor");
        map.put("mdtid", mdtid);
        map.put("type", type.toString());
        map.put("teachername", teachername);
        map.put("studentname", studentname);
        map.put("teachertel", teachertel);
        queue.offer(map);
    }

	public static void sendPurchaseSuccessPush(String telephone, String content, String title) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "purchasesuccesspush");
		map.put("telephone", telephone);
		map.put("content", content);
		map.put("title", title);
		queue.offer(map);
	}
    
    protected static Log logger = LogFactory.getLog(CMDDeal.class);

    public void execute() throws InterruptedException {
        while (true) {
            if (syssqs != null) {
                try {
                    // System.out.println("api"+Thread.currentThread().getId());
                    if (!queue.isEmpty()) {
                        Map<String, String> map = queue.poll();
                        if (map != null) {
                            // peopleid|objectid|telephone|mtdid
                            /*
                             * map.get("peopleid") .toString() + "|" +
                             * map.get("objectid").toString() + "|" +
                             * map.get("telephone").toString() + "|" +
                             */
                            // 通过mtdid去mongodb中查相对应的宝贝和监护人信息
                            String queuename = map.get("name").toString();
                            ObjectMapper mapper = new ObjectMapper();
                            mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII,
                                    true);
                            String jsonStr = mapper.writeValueAsString(map);
                            if (queuename.equals("listenin"))
                                syssqs.putString(queuename, jsonStr);
                            else if (queuename.equals("audio"))
                                syssqs.putString(queuename, jsonStr);
                            else if (queuename.equals("find")) {
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("location")) {
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("resetdevice")) {
                                Map map2 = new HashMap();
                                map2.put("name", "resetdevice");
                                map2.put("telephones", map.get("telephone").toString());
                                map2.put("familystatuses", map.get("familystatuses").toString());
                                map2.put("mtdid", map.get("mtdid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("remoteshutdown")) {
                                Map map2 = new HashMap();
                                map2.put("name", "remoteshutdown");
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("firewall")) {
                                Map map2 = new HashMap();
                                map2.put("name", "firewall");
                                map2.put("status", map.get("status").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("powerbtn")) {
                                Map map2 = new HashMap();
                                map2.put("name", "powerbtn");
                                map2.put("status", map.get("status").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("bluetooth")) {
                                Map map2 = new HashMap();
                                map2.put("name", "bluetooth");
                                map2.put("status", map.get("status").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("classsilent")) {
                                Map map2 = new HashMap();
                                map2.put("name", "classsilent");
                                map2.put("data", map.get("data").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("reportmode")) {
                                Map map2 = new HashMap();
                                map2.put("name", "reportmode");
                                map2.put("mode", map.get("mode").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("alarmclock")) {
                                Map map2 = new HashMap();
                                map2.put("name", "alarmclock");
                                map2.put("data", map.get("data").toString());
                                map2.put("mtdid", map.get("mdtid").toString());
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("renewal")) {
                                Map map2 = new HashMap();
                                map2.put("name", "renewal");
                                map2.put("fee", map.get("fee").toString());
                                map2.put("datetime", map.get("datetime").toString());
                                map2.put("endtime", map.get("endtime").toString());
                                map2.put("mdtid", map.get("mdtid").toString());
                                map2.put("telephone", map.get("telephone").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            }else if (queuename.equals("purchase")) {
                                Map map2 = new HashMap();
                                map2.put("name", "purchase");
                                map2.put("num1", map.get("num1").toString());
                                map2.put("num2", map.get("num2").toString());
                                map2.put("price1", map.get("price1").toString());
                                map2.put("price2", map.get("price2").toString());
                                map2.put("datetime", map.get("datetime").toString());
                                map2.put("telephone", map.get("telephone").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("notice")) {
                                Map map2 = new HashMap();
                                map2.put("name", "notice");
                                map2.put("notice", map.get("notice").toString());
                                map2.put("mdtid", map.get("mdtid").toString());
                                map2.put("teachername", map.get("teachername").toString());
                                map2.put("studentname", map.get("studentname").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("carefor")) {
                                Map map2 = new HashMap();
                                map2.put("name", "carefor");
                                map2.put("type", map.get("type").toString());
                                map2.put("mdtid", map.get("mdtid").toString());
                                map2.put("teachername", map.get("teachername").toString());
                                map2.put("studentname", map.get("studentname").toString());
                                if(map.containsKey("teachertel"))
                                    map2.put("teachertel", map.get("teachertel").toString());
                                else
                                    map2.put("teachertel","");
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("cancelbaby")) {
                                Map map2 = new HashMap();
                                map2.put("name", "cancelbaby");
                                map2.put("peopleid", map.get("peopleid").toString());
                                map2.put("objectid", map.get("objectid").toString());
                                map2.put("telephone", map.get("telephone").toString());
                                map2.put("mtdid", map.get("mtdid").toString());
                                map2.put("babyname", map.get("babyname").toString());
                                jsonStr = mapper.writeValueAsString(map2);
                                syssqs.putString(queuename, jsonStr);
                            } else if (queuename.equals("purchasesuccesspush")) {
                            	Map map2 = new HashMap();
                            	map2.put("name", "purchasesuccesspush");
                            	map2.put("title", map.get("title").toString());
                            	map2.put("telephone", map.get("telephone").toString());
                            	map2.put("content", map.get("content").toString());
                            	jsonStr = mapper.writeValueAsString(map2);
                            	syssqs.putString(queuename, jsonStr);
                            }
                        } else {
                            Thread.sleep(1000);
                        }
                    } else {
                        Thread.sleep(1000);
                    }
                } catch (HttpsqsException e) {
                    Thread.sleep(1000);
                    continue;
                } catch (Exception e) {
                    logger.error(e);
                    Thread.sleep(5000);
                    continue;
                }
            }
        }
    }
}
